package com.gm.wj.util;

import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;

/**
 * @description: 短信验证码
 * @author: Huang_shengjun
 * @create: 2020-09-08 18:16
 **/
public class SendByPhoneUtil {

    /**
     * twilio免费发送短信平台。但是只能发一个电话号码
     */
    private static final String accountSid = " "; // Your Account SID from www.twilio.com/user/account
    private static final String authToken = " "; // Your Auth Token from www.twilio.com/user/account

    public static void main(String[] args) {
    twilioPhoneCode(" ","您的验证码为：66666");
}

    public static String twilioPhoneCode(String toNum,String replyContent) {

        try{
            Twilio.init(accountSid, authToken);

            Message message = Message.creator(
                    new PhoneNumber("+86" + toNum),  // To number ,Phone number with area code
                    new PhoneNumber("+14044766564"),  // From number
                    "【教育培训机构】" + replyContent    // SMS body  发送主体内容
            ).create();
            System.out.println("messagetoString==" + message.toString());
            System.out.println("messagegetSid==" + message.getSid());

            if (! (message.getSid().isEmpty() )){
                System.out.println("短信发送成功-->" + message.getSid());
                return "1";
            }
            return "短信发送失败";
        }catch(Exception e){
            //如果手机号格式不正确，需要捕获异常提醒用户
            System.out.println("短信发送失败-->" + e.getMessage());
            return "短信发送失败-->" + e.getMessage();
        }
    }

}
